var cheobang_id="";
$(document).ready(function() {
	 idxcheck = function(aaaa){
			var idx = $('.pname').index(aaaa);
			$("#inputtag").val($('.pname').eq(idx).val());
			$("#namelist").hide();
		} 
	$(".inputtag").focus();
	$("#btncheckname").click(function(){
		//alert("sss");
		
		var name = $(".inputtag").val();
		 $.ajax({
			url : "wardsoonap.do",
			data : "NAME="+name,
			dataType : "json",
			success : function(data){
				$("#sidetable").empty();
				$("#sidetable").html("<tr><th>접수번호</th><th>이름</th></tr>");
				 for(var i=0;i<data.list.length;i++){
						var name=data.list[i].name;
						var jupsoo_id=data.list[i].jupsoo_id;
						var html = "<tr class='nameTr' ondblclick='dbclick(this);'>"+
						"<td><input type='hidden' class='selectjupsoo_id' readonly='readonly' value='"+jupsoo_id+"'>"+jupsoo_id+"</td>"+
						"<td><input type='hidden' class='selectname' readonly='readonly' value='"+name+"'>"+name+"</td></tr>";
						$('#sidetable').append(html);
				 }			
			},
			error:function(data){
				//alert("fail");
			}
		}); 
	});	
});		
var cheobang_id2="";
var jupsoo_id;
function dbclick(aaaa){
	var idx="";
	idx = $(".nameTr").index(aaaa);
	var name=$('.selectname').eq(idx).val();
	jupsoo_id = $('.selectjupsoo_id').eq(idx).val();
	//alert(jupsoo_id);
	$.ajax({
		url : "wardsoonapfinal.do",
		data : "jupsoo_id="+jupsoo_id,	
		dataType : "json",
		success : function(data){
			jupsoo_id=data.nosoonap.jupsoo_id;
			
			var html = /* "<tr id='patient_info_tr1'>" */
				"<td>" + data.patient.patient_id + "</td>" + "<td>"
						+ data.patient.name + "</td>" + "<td>"
						+ data.patient.gender + "</td>" + "<td>"
						+ data.patient.jumin1 + "-"
						+ data.patient.jumin2 + "</td>" + "<td>"
						+ data.patient.is_kuk + "</td>" + "<td>"
						+ data.patient.is_car + "</td>" + "<td>"
						+ data.patient.is_san + "</td>";
				/* +"</tr>"; */

				var html2 = /* "<tr id='patient_info_tr2'>" */
				"<td>"
						+ data.patient.zipcode1
						+ "-"
						+ data.patient.zipcode2
						+ "</td>"
						+ "<td>"
						+ data.patient.addr1
						+ " "
						+ data.patient.addr2
						+ "</td>"
						+ "<td>"
						+ data.patient.tel
						+ "</td>"
						/* +"</tr>" */
						+ "<input type='hidden' name='patient_id' value='"+data.patient.patient_id+"'>";
				$('#patient_info_tr1').empty().append(html);
				$('#patient_info_tr2').empty().append(html2);
			
			
			/*var html = "<td id='p1'>"+data.patient.patient_id+"</td>"+
			"<td id='p2'>"+data.patient.name+"</td>"+
			"<td id='p3'>"+data.patient.gender+"</td>"+
			"<td id='p4'>"+data.patient.jumin1+"-"+data.patient.jumin2+"</td>"+
			"<td id='p5'>"+data.patient.is_kuk+"</td>"+
			"<td id='p6'>"+data.patient.is_car+"</td>"+
			"<td id='p7'>"+data.patient.is_san+"</td>"+
			"<td id='p8'>"+data.patient.zipcode1+"</td>"+
			"<td id='p9'>"+data.patient.zipcode2+"</td>"+
			"<td id='p10'>"+data.patient.addr1+"</td>"+
			"<td id='p11'>"+data.patient.addr2+"</td>"+
			"<td id='p12'>"+data.patient.tel+"</td>"+
			"<input type='hidden' name='patient_id' value='"+data.patient.patient_id+"'> ";
			$("#pinfo").html(html);*/
			
			$("#righttable3").html("<tr id='sootitle'><th>접수번호</th><th>접수일자</th><th>전체금액</th><th>미수납금액</th></tr>");
				var html4 ="<tr>"+
				"<td>"+data.nosoonap.jupsoo_id+"</td>"+
				"<td>"+data.nosoonap.jupsoo_date+"</td>"+
				"<td>"+data.nosoonap.total_sg_price+"</td>"+
				"<td>"+data.nosoonap.remainder_price+"</td>"+
				"</tr>"+
				"<tr id='sootitle'>"+
				"<td colspan ='2'>수납 금액</td><td colspan='2'><input id='no_soonap_btn' name='no_soonap_btn' type='text' placeholder='수납금액 입력란'></td>"+
				"</tr><tr>"+
					"<td colspan='4'><button id='finalbtn' class='btn2' onclick='formaction()'>완료</button></td>"+
				"</tr>"+
				"<input type='hidden' name='jupsoo_id' value='" + jupsoo_id + "'>";
				$("#righttable3").append(html4);
		}
	});
};

function formaction(){
		var no_soonap_btn=$("#no_soonap_btn").val();
		soonapform.action="wardnosoonapsubmit.do";
		soonapform.submit();
};

var name;
function fn_onkeypress(){
	name = $(".inputtag").val();
	$("#namelist").show();
	if(name == ""){
		$("#namelist").hide();
	}
	//alert(name);
	var namelist;
	//alert(name);
		$.ajax({
			url : "wardcheobangcheck2.do",
			data : "NAME="+name,	
			dataType : "json",
			success : function(data){
				//alert(data.list[0].name);
				$("#namelist").empty();
				//alert(data.list[0].name);
				if(typeof namelist == "undefined"){
					namelist="";
				}
				for(var k=0; k<data.list.length;k++){
					namelist = "<div class='patient_name' name='patient_name'><input type='text' class='pname' name='pname' value='"+data.list[k].name+"' readonly='readonly' onclick='idxcheck(this);'></div>";	
					$("#namelist").append(namelist);
				}
			},
			error:function(data){
				//alert("fail");
			}
		});
}
